<?php
include('/var/www/html/new/d/module/api/HuaQiaoCheng/hqc.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");  
	$le=new go_sql();
	$le->connect();
}
$PFT_infunction = new AllFunction();
$PFT_infunction->logfile('15|'.json_encode($_REQUEST),'get_data');

if(!isset($_REQUEST['Action']) && $_REQUEST['PayStatus'] != 2){
	
	$TransactionCode = $_REQUEST['PayStatus'] == 1 ? "03" : "04" ; 	
	if(!$_REQUEST['TicketID']) exit("101|0|0|0|0|0");
	$tid = $_REQUEST['TicketID'] + 0; //门票id
	$select_uuid = "select t.uuid,t.pid,l.apply_did,m.account from uu_jq_ticket as t, uu_land as l,pft_member as m where l.id = t.landid and t.id=$tid and l.apply_did=m.id limit 1";
	
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit("101");		
	$uuid = $GLOBALS['le']->f('uuid');  //查询票类id	
	$apply_did = $GLOBALS['le']->f('apply_did');
	$lid = $GLOBALS['le']->f('landid'); 
	$pid = $GLOBALS['le']->f('pid');
	$account = $GLOBALS['le']->f('account'); 
	
	$get_ghprice = $PFT_infunction->Dynamic_Price_And_Storage($account,$pid,substr($_REQUEST['PlayDate'],0,10),0,$apply_did);  //获取供货价
	$get_ghprice_xml = simplexml_load_string($get_ghprice);
	$ghprice = (string)$get_ghprice_xml->Rec->UUsprice / 100 ;
	
	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));
	$array = array(
		'OutOrderId'	=> $ls_order ,
		'Name'			=> $_REQUEST['TouristName'],
		'Mobile'		=> $_REQUEST['TouristTel'],
		'Quantity'		=> $_REQUEST['Tnum'],
		'GoodsId'		=> $uuid ,
		'SalePrice'		=> $ghprice ,
		'CertificateNum'=> $_REQUEST['IdCard'],
		'AppointTripDate'=> substr($_REQUEST['PlayDate'],0,10)
	);
	$hqc = new HuaQiaoCheng($apply_did);
	$postOrder = $hqc->PayedOrder($array,$TransactionCode,$PFT_infunction); 
	if($postOrder != 101){
		$apiorder = (string)$postOrder->OtmOrderId;
		$apicode  = (string)$postOrder->Vouchers->Voucher->VoucherValue;
		if($TransactionCode == "04"){ 
			$apiorder = (string)$postOrder->ApOrderId;
			$apicode =	"0" ;
		}
		exit("200|0|0|0|".$apicode."|".$apiorder."_".$ls_order);
	}
	else exit("101");
	
}
elseif($_REQUEST['Action'] == 'Relation'){ //下单成功

	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($Ordern);
	$apicode = (string)$pft_order_xml->Rec->UUvcode;
	$apiorders = explode("_",(string)$pft_order_xml->Rec->UUtordernum);
	$apiorder  = $apiorders[0];
	$tempOrder = $apiorders[1];
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	
	$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$apiorder','$tempOrder',15,0,$tnum,'$apicode',0,now())";
	$GLOBALS['le']->query($str);
	
}elseif($_REQUEST['Action'] == 'MOD'){//修改订单

	$pftOrder = $_REQUEST['Ordern'];
	$selapiOrder = "select apiOrder,tempOrder,oStatus,oStnum,apiCode from all_api_order where pftOrder='$pftOrder' limit 1";
	$GLOBALS['le']->query($selapiOrder);
	if(!$GLOBALS['le']->fetch_assoc())	exit('105');
	$tempOrder = $GLOBALS['le']->f('tempOrder');
	$apiOrder = $GLOBALS['le']->f('apiOrder');
	$oStatus = $GLOBALS['le']->f('oStatus');
	$oStnum = $GLOBALS['le']->f('oStnum');
	$apiCode = $GLOBALS['le']->f('apiCode');
	
	$pft_order_xml = $PFT_infunction->Select_PFTorder($pftOrder);
	$tid = (int)$pft_order_xml->Rec->UUtid;
	
	$select_uuid = "select l.apply_did from uu_jq_ticket as t, uu_land as l where l.id = t.landid and t.id=$tid limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit("101");		
	$apply_did = $GLOBALS['le']->f('apply_did');
	
	$hqc = new HuaQiaoCheng($apply_did);
	
	$rnum = $_REQUEST['Tnum'] == 0 ? $oStnum : $oStnum - $_REQUEST['Tnum'] ;
	
	$modify = array(
		'pftOrder'	=> $pftOrder,
		'OutOrderId' => $tempOrder,
		'OtmOrderId' => $apiOrder,
		'VoucherValue'	=> $apiCode,
		'RefundQuantity' => $rnum 
	);
	$oStatus = 4;
	if($_REQUEST['Tnum'] == 0) $oStatus = 3;
	
	$ModifyOrder = $hqc->ModifyOrder($modify,$PFT_infunction);
	if($ModifyOrder == 100){
		$str="update all_api_order set oStatus=$oStatus,oStnum=".$_REQUEST['Tnum'].", handleStatus=0 where pftOrder='".$_REQUEST['Ordern']."' limit 1";
		$GLOBALS['le']->query($str);	
		exit("200");
	}
	else{
		
		$str="update all_api_order set oStatus=$oStatus,oStnum=".$_REQUEST['Tnum'].", handleStatus=1 where pftOrder='".$_REQUEST['Ordern']."' limit 1";
		$GLOBALS['le']->query($str);
		exit("1077");
		
	}
}
else exit("101|0|0|0|0|0");
